<?php
require_once("lib/api.php");
require_once("lib/auth/session.php");

$api = new api();
/*
 if (isset($_POST['userid'])) {
 $userid = $_POST['userid'];
 if (!is_numeric($userid)) {
 $api->setYouFail("Needs to be a number.");
 } elseif (!($userid > 0)) {
 $api->setYouFail("Needs to be a number larger than 0.");
 } else
 */
if (!loggedIn()) {//, JSON_FORCE_OBJECT
    if ($redir = authenticate()) {
        $api->setOK();
        $api->setReturn("redirect",$redir);
        $api->setReturn("sid",$GLOBALS["TOKEN"]);//needed?
     }elseif ($return = login()) {
        $api->setOK("Logged in.");
        header("location: $return");//?sid=".$GLOBALS["TOKEN"]
    } else {
        $api->setFail("Not authenticated.");
    }
} else {
    $api->setOK("Already logged in");
    $api->setReturn("userid",loggedIn());
}

//header("Location: ".$_GET['return']);
$api->returnResponse();
?>
